package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.bo.CategoryCreateBo;
import com.cskaoyan.market.bo.CategoryDeleteBo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketCategoryVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController  {

    @Autowired
    AdminCategoryService adminCategoryService;
    @Autowired
    SecurityManager securityManager;

    @GetMapping("list")
    @RequiresPermissions("admin:category:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"},button = "查询")
    public Object list()  {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        List<MarketCategoryVO> categoryVOList = adminCategoryService.list();
        //封装成VOlist对象处理
        session.setAttribute("categoryVOList", categoryVOList);
        return ResponseUtil.okList(categoryVOList);
    }

    @GetMapping("l1")
    public Object l1() {
        //查level为L1的为一级类目
        List<Map<String, Object>> l1List = new ArrayList<>();
        List<MarketCategory> categoryList = adminCategoryService.l1();
        for (MarketCategory category : categoryList) {
            Map<String, Object> l1Map = new HashMap<>();
            l1Map.put("value", category.getId());
            l1Map.put("label", category.getName());
            l1List.add(l1Map);
        }
        return ResponseUtil.okList(l1List);
    }

    @PostMapping("create")
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"},button = "添加")
    public Object create(@RequestBody CategoryCreateBo categoryCreateBo) {
        MarketCategory marketCategory = adminCategoryService.create(categoryCreateBo);
        //转换时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String addTimeToString = marketCategory.getAddTime().format(formatter);
        String UpdateTimeToString = marketCategory.getUpdateTime().format(formatter);

        Map<String, Object> data = new HashMap<>();
        data.put("id", marketCategory.getId());
        data.put("name", marketCategory.getName());
        data.put("desc", marketCategory.getDesc());
        data.put("keywords", marketCategory.getKeywords());
        data.put("level", marketCategory.getLevel());
        data.put("pid", marketCategory.getPid());
        data.put("addTime", addTimeToString);
        data.put("updateTime", UpdateTimeToString);
        if (!StringUtils.isEmpty(marketCategory.getPicUrl())) {
            data.put("picUrl", marketCategory.getPicUrl());
        }
        if (!StringUtils.isEmpty(marketCategory.getIconUrl())) {
            data.put("iconUrl", marketCategory.getIconUrl());
        }
        return ResponseUtil.ok(data);
    }

    @PostMapping("delete")
    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"},button = "删除")
    public Object delete(@RequestBody CategoryDeleteBo categoryDeleteBo)  {
        String id = categoryDeleteBo.getId().toString();
        adminCategoryService.delete(id);
        return ResponseUtil.ok();
    }


    @PostMapping("update")
    @RequiresPermissions("admin:category:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"},button = "编辑")
    public Object update(@RequestBody CategoryDeleteBo categoryDeleteBo)  {
        //读取请求体中的内容
        adminCategoryService.update(categoryDeleteBo);
        return ResponseUtil.ok();
    }
}
